package dth.bean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;

import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;

import dth.dao.DthContactDAO;
import dth.entity.DthContact;
import dth.utils.DthUtil;
@ManagedBean(name="aboutUsBean")
public class AboutUsBean {
	private int id;
	private int fk_admin_id;
	private String address;
	private String phoneNumber;
	private String email;
	private double latitute;
	private double longtitute;
	private Date dateInsert;
	private MapModel model = new DefaultMapModel();
	private List<DthContact> listDthContacts=new ArrayList<DthContact>();
	public AboutUsBean() {
		// TODO Auto-generated constructor stub
		System.out.println("----about us bean");
		List<DthContact> list=getListDthContacts();
		if(list!=null){
			for(DthContact contact:list){
				this.id=contact.getDthId();
				this.address=contact.getDthAddress();
				this.phoneNumber=contact.getDthContactNumber();
				this.email=contact.getEmail();
				this.latitute=contact.getLatitute();
				this.longtitute=contact.getLongtitute();
				this.fk_admin_id=contact.getFkAdminId();
			}
		}
	}
	public List<DthContact> getListDthContacts() {
		DthContactDAO contactDAO=new DthContactDAO();
		listDthContacts=contactDAO.getDthContact();
		System.out.println("---dth contact size:"+listDthContacts.size());
		
		
		return listDthContacts;
	}

	public void setListDthContacts(List<DthContact> listDthContacts) {
		this.listDthContacts = listDthContacts;
	}

	public MapModel getModel() {
		System.out.println("----get model");
		if(getListDthContacts()!=null){
			for(DthContact contact:getListDthContacts())
				model.addOverlay(new Marker(new LatLng(contact.getLatitute(),contact.getLongtitute()),"DTH headquarter"));
		}
		return model;
	}
	public void update(){
		DthContact contact=new DthContact();
		contact.setDateInsert(DthUtil.convertToSqlDate(new java.util.Date()));
		contact.setDthAddress(address);
		contact.setDthContactNumber(phoneNumber);
		contact.setDthId(id);
		contact.setEmail(email);
		contact.setFkAdminId(fk_admin_id);
		contact.setLatitute(latitute);
		contact.setLongtitute(longtitute);
		try{
			DthContactDAO contactDAO=new DthContactDAO();
			int count=contactDAO.edit(contact);
			if (count > 0)
			{
				DthUtil.navigateTo("aboutus.xhtml");
				DthUtil.showMessage("Mesage", "Update success");
				
			}
			else
				DthUtil.showMessage("Message", "Update error");
		}
		catch(Exception ex){
			DthUtil.showMessage("Message", "Update error");
		}
		
	}
	public void setModel(MapModel model) {
		this.model = model;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhoneNumber() {
		return phoneNumber;
	}
	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public double getLatitute() {
		return latitute;
	}
	public void setLatitute(double latitute) {
		this.latitute = latitute;
	}
	public double getLongtitute() {
		return longtitute;
	}
	public void setLongtitute(double longtitute) {
		this.longtitute = longtitute;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getFk_admin_id() {
		return fk_admin_id;
	}
	public void setFk_admin_id(int fk_admin_id) {
		this.fk_admin_id = fk_admin_id;
	}
	
}
